# !/usr/bin/env python
# -*- coding: utf-8 -*-
# @Copyright © 启明星辰 版权所有
# @Time    : 2023/7/28 09:42
# @Author  : 胡浩浩
# @File    : redis_tools.py
# @IDE     : PyCharm
# @description :
from redis import StrictRedis


class RedisClient:
    def __init__(self):
        self.redis = StrictRedis(
            host="192.168.114.86",
            port=6379,
            db=1,
            password="sa_1234567",
            socket_connect_timeout=300,
            retry_on_timeout=True,
        )

    def add(self, version, product_name,data_list):
        """
            key  api:TSOC-CSA:v3.0.500.5.2.0
            data_list ['/csa/api/v1/appIntegration/interface/instance/edit', '/csa/api/v1/appIntegration/interface/appList']
        """
        key = "api:" + version.split("_")[0].replace(" ", ":")
        print(key)
        # 删除原有的数据
        self.redis.delete(key)
        # 再添加新的数据到Redis里面
        self.redis.sadd(key, *[product_name+data for data in data_list])
        print("同步完成")
        self.redis.close()

    def get(self,key):
        data=self.redis.smembers(key)
        # print(data)
        data_list=[]
        for d in data:
            data_list.append(d.decode("utf-8"))
        # print(data_list)
        return data_list

if __name__ == '__main__':
    data=RedisClient().get("api:TSOC-CSA:v3.0.500.5.2.0")
    for i in data:
        if "{" in i:
            print(i)